/* 
 * Copyright (C) 2014 Realtek Semiconductor Corp.
 * All Rights Reserved.
 * 
 * This program is the proprietary software of Realtek Semiconductor
 * Corporation and/or its licensors, and only be used, duplicated,
 * modified or distributed under the authorized license from Realtek.
 * 
 * ANY USE OF THE SOFTWARE OTHER THAN AS AUTHORIZED UNDER
 * THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 * 
 */


#include "app_basic.h"
#include "omci_task.h"
#include "omci_mic.h"
#include <stdlib.h>

#include "util_aes.h"
#include <sys/resource.h>

MIB_TABLE_INFO_T gMibEnhancedSecCtlTableInfo;
MIB_ATTR_INFO_T  gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ATTR_NUM];
MIB_TABLE_ENHANCEDSECCTL_T gMibEnhancedSecCtlDefRow;
MIB_TABLE_OPER_T gMibEnhancedSecCtlOper;

extern omci_mulget_info_ts gOmciMulGetData[OMCI_MSG_BASELINE_PRI_NUM];
UINT8	MSK[16];
UINT8	MSKname_string[16]= {0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, 0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93};	
static GOS_ERROR_CODE EnhancedSecCtlTmrProc(MIB_TABLE_ENHANCEDSECCTL_T *pEsc, UINT32 state);

void print_hex(uint8_t *buf, int32_t len)
{
	int32_t i;
	for(i = 0; i < len; i++) {
		printf("%02x", buf[i]);
		if((i % 4) == 3) 
			printf(" ");
		if((i % 16) == 15) 
			printf("\n");    
	}
	printf("\n");
}

static void OltCryptoCapDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	UINT16 i;

	printf("OltCryptoCap: ");
	for(i = 0; i < MIB_TABLE_OLTCRYPTOCAP_LEN; i++)
		printf(" %02x", pEnhancedSecCtl->OltCryptoCap[i]);
	printf("\n");
	return;
}

static void OltRamChallTblDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	oltRamChallTblEntry_t *pEntry = NULL;
	omci_olt_ram_chall_entry_t *pRowEntry = NULL;
	UINT16 count=0, i;

	OMCI_PRINT("OltRamChallTbl (%u)", pEnhancedSecCtl->curOltRamChallCnt);
	LIST_FOREACH(pEntry, &pEnhancedSecCtl->oltRamChallHead, entries)
	{
		pRowEntry = &pEntry->tableEntry;
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("INDEX:\t\t\t%u", count);
		OMCI_PRINT("Row Num:\t\t%u", pRowEntry->rowNum);
		printf("Content:");
		for(i = 0; i < MIB_TABLE_OLTRAMCHALLTABLE_CONTENT_LEN; i++)
			printf(" %02x", pRowEntry->content[i]);
		count++;
		printf("\n");
	}
	if(0 == count)
	{
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("No entry");
	}
	return;
}

static void OnuRamChallTblDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	onuRamChallTblEntry_t *pEntry = NULL;
	omci_onu_ram_chall_entry_t *pRowEntry = NULL;
	UINT16 count=0, i;

	OMCI_PRINT("OnuRamChallTbl (%u)", pEnhancedSecCtl->curOnuRamChallCnt);
	LIST_FOREACH(pEntry, &pEnhancedSecCtl->onuRamChallHead, entries)
	{
		pRowEntry = &pEntry->tableEntry;
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("INDEX:\t\t\t%u", count);
		printf("Content:");
		for(i = 0; i < MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN; i++)
			printf(" %02x", pRowEntry->content[i]);
		count++;
		printf("\n");
	}
	if(0 == count)
	{
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("No entry");
	}
	return;
}

static void OnuAuthRslTblDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	onuAuthRslTblEntry_t *pEntry = NULL;
	omci_onu_auth_rsl_entry_t *pRowEntry = NULL;
	UINT16 count=0, i;

	OMCI_PRINT("OnuAuthRslTbl (%u)", pEnhancedSecCtl->curOnuAuthRslCnt);
	LIST_FOREACH(pEntry, &pEnhancedSecCtl->onuAuthRslHead, entries)
	{
		pRowEntry = &pEntry->tableEntry;
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("INDEX:\t\t\t%u", count);
		printf("Content:");
		for(i = 0; i < MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN; i++)
			printf(" %02x", pRowEntry->content[i]);
		count++;
		printf("\n");
	}
	if(0 == count)
	{
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("No entry");
	}
	return;
}

static void OltAuthRslTblDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	oltAuthRslTblEntry_t *pEntry = NULL;
	omci_olt_auth_rsl_entry_t *pRowEntry = NULL;
	UINT16 count=0, i;

	OMCI_PRINT("OltAuthRslTbl (%u)", pEnhancedSecCtl->curOltAuthRslCnt);
	LIST_FOREACH(pEntry, &pEnhancedSecCtl->oltAuthRslHead, entries)
	{
		pRowEntry = &pEntry->tableEntry;
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("INDEX:\t\t\t%u", count);
		OMCI_PRINT("Row Num:\t\t%u", pRowEntry->rowNum);
		printf("Content:");
		for(i = 0; i < MIB_TABLE_OLTAUTHRSLTABLE_CONTENT_LEN; i++)
			printf(" %02x", pRowEntry->content[i]);
		count++;
		printf("\n");
	}
	if(0 == count)
	{
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("No entry");
	}
	return;
}

static void MasterSessKeyNameDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	UINT16 i;

	printf("MasterSessKeyName: ");
	for(i = 0; i < MIB_TABLE_MASTERSESSKEYNAME_LEN; i++)
		printf(" %02x", pEnhancedSecCtl->MasterSessKeyName[i]);
	printf("\n");
	return;
}

static void BcastKeyTblDump(MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl)
{
	bcastKeyTblEntry_t *pEntry = NULL;
	omci_bcast_key_entry_t *pRowEntry = NULL;
	UINT16 count=0, i;

	OMCI_PRINT("BcastKeyTbl (%u)", pEnhancedSecCtl->curBcastKeyCnt);
	LIST_FOREACH(pEntry, &pEnhancedSecCtl->bcastKeyHead, entries)
	{
		pRowEntry = &pEntry->tableEntry;
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("INDEX:\t\t\t%u", count);
	//	OMCI_PRINT("Row Control:\t\t%x", pRowEntry->rowCtl.val);
	//	OMCI_PRINT("Row Identifier:\t\t%x", pRowEntry->rowId.val);
		printf("Content:");
		for(i = 0; i < MIB_TABLE_BCASTKEY_CONTENT_LEN; i++)
			printf(" %02x", pRowEntry->content[i]);
		count++;
		printf("\n");
	}
	if(0 == count)
	{
		OMCI_PRINT("----------------------------------");
		OMCI_PRINT("No entry");
	}
	return;
}

static GOS_ERROR_CODE EscSendAvc(MIB_TABLE_ENHANCEDSECCTL_T *pEsc, UINT32 attrIdx, UINT32 size)
{
    GOS_ERROR_CODE              ret = GOS_OK;
    omci_msg_norm_t             *pOmciMsg;
    MIB_TABLE_INDEX             tableIndex = MIB_TABLE_ENHANCEDSECCTL_INDEX;
    MIB_ATTRS_SET               attrSet = 0;               
    omci_msg_attr_mask_t		avcOmciSet;

    pOmciMsg = malloc(sizeof(omci_msg_norm_t));
    if (!pOmciMsg)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Allocate memory for test handler fail: %s",
        MIB_GetTableName(tableIndex));

        return GOS_FAIL;
    }

    memset(pOmciMsg, 0x0, sizeof(omci_msg_norm_t));
    pOmciMsg->db     = 0;
    pOmciMsg->ar     = 0;
    pOmciMsg->ak     = 0;
    pOmciMsg->devId  = OMCI_MSG_BASELINE_DEVICE_ID;
    pOmciMsg->type   = OMCI_MSG_TYPE_ATTR_VALUE_CHANGE;
    pOmciMsg->meId.meClass = OMCI_ME_CLASS_ENHANCED_SECURITY_CONTROL;

    // send the AVC 
    MIB_SetAttrSet(&attrSet, attrIdx);
    OMCI_AttrsMibSetToOmciSet(&attrSet, &avcOmciSet);
    GOS_SetUINT16((UINT16*)&pOmciMsg->content[0], GOS_Htons(avcOmciSet));

    if(MIB_ATTR_TYPE_TABLE == MIB_GetAttrDataType(tableIndex, attrIdx))
        GOS_SetUINT32((UINT32*)&pOmciMsg->content[2], GOS_Htonl(size));
    else
        MIB_GetAttrFromBuf(tableIndex, attrIdx, &pOmciMsg->content[2], pEsc, MIB_GetAttrLen(tableIndex, attrIdx));

    ret = OMCI_SendMsg(OMCI_APPL, OMCI_TX_OMCI_MSG, OMCI_MSG_PRI_NORMAL, pOmciMsg, sizeof(omci_msg_norm_t));
    if (GOS_OK != ret)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Unable to send out test result: %s", MIB_GetTableName(tableIndex));
        ret = GOS_FAIL;
    }

    // free allocated memory before leaving
    free(pOmciMsg);
    return ret;
}

static GOS_ERROR_CODE OnuRamChallEntryClear(MIB_TABLE_ENHANCEDSECCTL_T*pEsc)
{
	onuRamChallTblEntry_t *pEntry;
	pEntry = LIST_FIRST(&pEsc->onuRamChallHead);
	while(pEntry!=NULL)
	{
		LIST_REMOVE(pEntry,entries);
		free(pEntry);
		pEntry = LIST_FIRST(&pEsc->onuRamChallHead);
	}
	LIST_INIT(&pEsc->onuRamChallHead);
	pEsc->curOnuRamChallCnt = 0;
	return GOS_OK;
}

static GOS_ERROR_CODE OnuRamChallEntryGen(MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
	onuRamChallTblEntry_t *pEntry;
	UINT32 rand;
	int i, j;

	for(i = 0; i < MIB_TABLE_ONURAMCHALLTABLE_CNT; i++)
	{
		pEntry = (onuRamChallTblEntry_t*)malloc(sizeof(onuRamChallTblEntry_t));
		if(!pEntry)
		{
			OMCI_LOG(OMCI_LOG_LEVEL_ERR,"Malloc onuRamChall Table Entry Fail");
			return GOS_FAIL;
		}

		for(j = 0; j < 4; j++)
		{
			rand = random();
			memcpy(&pEntry->tableEntry.content[j*4], &rand, sizeof(rand));
		}
		LIST_INSERT_HEAD(&pEsc->onuRamChallHead, pEntry, entries);
		pEsc->curOnuRamChallCnt++;
	}

	return GOS_OK;
}


static GOS_ERROR_CODE OnuAuthRslGen(MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
	UINT32                     ret = GOS_OK;
	size_t                       msgIdx;
	oltRamChallTblEntry_t	*pOltRamChall;
	onuRamChallTblEntry_t	*pOnuRamChall;
	onuAuthRslTblEntry_t 	*pOnuAuthRsl;
	UINT8     message[128];
	UINT8     mact[64];
	UINT8	PSK[64];
	
	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s...", __FUNCTION__);
	if(NULL == pEsc)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Unable to get Table Ptr of ENHANCEDSECCTL");
		return GOS_FAIL;
	}

	if(LIST_EMPTY(&pEsc->oltRamChallHead))
	{
		OMCI_LOG(OMCI_LOG_LEVEL_WARN, "OLTRAMCHALLTABLE is Empty, Unable to do authenticate.");
		return GOS_FAIL;
	}

	if(LIST_EMPTY(&pEsc->onuRamChallHead))
	{
		OMCI_LOG(OMCI_LOG_LEVEL_WARN, "ONURAMCHALLTABLE is Empty, Unable to do authenticate.");
		return GOS_FAIL;
	}

	/* ONU_selected_crypto capabilities */
	msgIdx = 0;
	message[msgIdx] = pEsc->OnuSelCryptoCap;
	msgIdx++;

	/* OLT_challenge  */	
	LIST_FOREACH(pOltRamChall, &pEsc->oltRamChallHead, entries)
	{
		memcpy(&message[msgIdx], pOltRamChall->tableEntry.content, MIB_TABLE_OLTRAMCHALLTABLE_CONTENT_LEN); 		
		msgIdx += MIB_TABLE_OLTRAMCHALLTABLE_CONTENT_LEN;
	}

	/* ONU_challenge  */	
	LIST_FOREACH(pOnuRamChall, &pEsc->onuRamChallHead, entries)
	{
		memcpy(&message[msgIdx], pOnuRamChall->tableEntry.content, MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN); 		
		msgIdx += MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN;
	}

	/* Peer_1_identity = "0x0000 0000 0000 0000"   */	
	memset(&message[msgIdx],0x0, PEER_1_IDENTITY_LEN);
	msgIdx += PEER_1_IDENTITY_LEN;	

	memcpy(PSK, gInfo.psk, 16);
	// Caculate the ONT auth result
	aes128_cmac_compute(PSK, message, msgIdx, 16, mact);


	/* update ONU Auth Result Table */
	pOnuAuthRsl = LIST_FIRST(&pEsc->onuAuthRslHead);
	if(NULL != pOnuAuthRsl)
	{
		memcpy(pOnuAuthRsl->tableEntry.content, mact, MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN);
	}
	else
	{
		pOnuAuthRsl = (onuAuthRslTblEntry_t*)malloc(sizeof(onuAuthRslTblEntry_t));
		if(!pOnuAuthRsl)
		{
			OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Malloc onuAuthRsl Table Entry Fail");
			return GOS_FAIL;
		}

		memcpy(pOnuAuthRsl->tableEntry.content, mact, MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN);
		LIST_INSERT_HEAD(&pEsc->onuAuthRslHead, pOnuAuthRsl, entries);
		pEsc->curOnuAuthRslCnt++;
	}


	/* calculate the MSK */
	msgIdx -= (PEER_1_IDENTITY_LEN + 1);	//only take the OLT Challenge and ONT Challenge table for MSK generation
	aes128_cmac_compute(PSK, &message[1], msgIdx, 16, mact);
	memcpy(MSK, mact, MIB_TABLE_MASTERSESSKEYNAME_LEN);


	/* calculate the MSK name*/
	memcpy(&message[msgIdx + 1], MSKname_string, 16);
	msgIdx += 16;
	aes128_cmac_compute(PSK, &message[1], msgIdx, 16, mact);
	memcpy(pEsc->MasterSessKeyName, mact, MIB_TABLE_MASTERSESSKEYNAME_LEN);

	return ret;
}

static GOS_ERROR_CODE OltAuthRslChk(MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
    size_t                       msgIdx;
    oltRamChallTblEntry_t	*pOltRamChall;
    onuRamChallTblEntry_t	*pOnuRamChall;
    oltAuthRslTblEntry_t 	*pOltAuthRsl;
    UINT8     message[128];
    UINT8     mact[64];
    UINT8	PSK[64];

    OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s...", __FUNCTION__);
    if(NULL == pEsc)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Unable to get Table Ptr of ENHANCEDSECCTL");
        return GOS_FAIL;
    }

    if(LIST_EMPTY(&pEsc->oltRamChallHead))
    {
        OMCI_LOG(OMCI_LOG_LEVEL_WARN, "OLTRAMCHALLTABLE is Empty, Unable to do authenticate.");
        return GOS_FAIL;
    }

    if(LIST_EMPTY(&pEsc->onuRamChallHead))
    {
        OMCI_LOG(OMCI_LOG_LEVEL_WARN, "ONURAMCHALLTABLE is Empty, Unable to do authenticate.");
        return GOS_FAIL;
    }

    if(LIST_EMPTY(&pEsc->oltAuthRslHead))
    {
        OMCI_LOG(OMCI_LOG_LEVEL_WARN, "OLTAUTHRSLTABLE is Empty, Unable to do authenticate.");
        return GOS_FAIL;
    }

    /* ONU_selected_crypto capabilities */
    msgIdx = 0;
    message[msgIdx] = pEsc->OnuSelCryptoCap;
    msgIdx++;

    /* ONU_challenge  */
    LIST_FOREACH(pOnuRamChall, &pEsc->onuRamChallHead, entries)
    {
        memcpy(&message[msgIdx], pOnuRamChall->tableEntry.content, MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN);
        msgIdx += MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN;
    }

    /* OLT_challenge  */
    LIST_FOREACH(pOltRamChall, &pEsc->oltRamChallHead, entries)
    {
        memcpy(&message[msgIdx], pOltRamChall->tableEntry.content, MIB_TABLE_OLTRAMCHALLTABLE_CONTENT_LEN);
        msgIdx += MIB_TABLE_OLTRAMCHALLTABLE_CONTENT_LEN;
    }

    /* Peer_2_identity = ONU Serial Number   */
    memcpy(&message[msgIdx], gInfo.sn, PEER_1_IDENTITY_LEN);
    msgIdx += PEER_1_IDENTITY_LEN;

    memcpy(PSK, gInfo.psk, 16);
    // Caculate the ONT auth result
    aes128_cmac_compute(PSK, message, msgIdx, 16, mact);

    /* compare OLT's auth result with local generated */
    pOltAuthRsl = LIST_FIRST(&pEsc->oltAuthRslHead);
    if(NULL == pOltAuthRsl)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "oltAuthRsl Table Entry is NULL!!");
        return GOS_FAIL;
    }

    if (memcmp(pOltAuthRsl->tableEntry.content, mact, MIB_TABLE_OLTAUTHRSLTABLE_CONTENT_LEN) == 0)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "OLT Auth result verification: Passed!!");
    }
    else
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR, "OLT Auth result verification: Failed!!");
        return GOS_FAIL;
    }

    return GOS_OK;
}

static GOS_ERROR_CODE EnhancedSecCtlRsl(MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s...", __FUNCTION__);

	/* clear and generate new Random Challenge Table */
	if(NULL == pEsc)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Unable to get Table Ptr of ENHANCEDSECCTL");
		return GOS_FAIL;
	}
	if((GOS_FAIL == OnuRamChallEntryClear(pEsc)) || (GOS_FAIL == OnuRamChallEntryGen(pEsc)))
		return GOS_FAIL;


	/* generate ONU auth result */
	if(GOS_FAIL == OnuAuthRslGen(pEsc))
		return GOS_FAIL;

    return GOS_OK;
}

static void EnhancedSecCtlTmrHdlr(UINT16   classID,
                                                UINT16  instanceID,
                                                UINT32  privData)
{
	MIB_TABLE_ENHANCEDSECCTL_T *pMibEsc = NULL,tmpEsc;

	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s..%d.", __FUNCTION__, __LINE__);

	tmpEsc.EntityId = instanceID;
	if(!mib_FindEntry(MIB_TABLE_ENHANCEDSECCTL_INDEX,&tmpEsc,&pMibEsc))
	{
		OMCI_LOG(OMCI_LOG_LEVEL_WARN,"can't find EhnSecCtrl 0x%x", instanceID);
		return ;
	}

	if(NULL == pMibEsc)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Unable to get Table Ptr of ENHANCEDSECCTL");
		return ;
	}

	if(privData == ENHANCEDSECCTL_TIMER_STATE_AUTH_RESULT)
	{
		/* Send AVC of ONURAMCHALLTABLE */
		EscSendAvc(pMibEsc, MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX, MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN * pMibEsc->curOnuRamChallCnt);

		/* Send AVC of ONUAUTHRSLTABLE */
		EscSendAvc(pMibEsc, MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX, MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN * pMibEsc->curOnuAuthRslCnt);
	}
    else if(privData == ENHANCEDSECCTL_TIMER_STATE_AUTH_STATUS)
    {
        if(ONU_AUTH_RESULT_SUCCESS == pMibEsc->OnuAuthSt)
	{
		//apply the new MSK to PONMAC, for new key generation
		omci_wrapper_setMSK(MSK);
        }

		//reset the default OMCI IK state. To generate OMCI IK again, if ONU state is restarted.
		gInfo.defaultOmciIkState = FALSE;

		/* Send AVC of ONU authentication status */
		EscSendAvc(pMibEsc, MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX, 0);
	}

	return ;
}

static GOS_ERROR_CODE EnhancedSecCtlTmrProc(MIB_TABLE_ENHANCEDSECCTL_T *pEsc, UINT32 state)
{
	GOS_ERROR_CODE      ret = GOS_OK;
	omci_timer_entry_t  *pEntry;

	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s..%d.", __FUNCTION__, __LINE__);

	if(NULL == pEsc)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR, "%s..%d: Unable to get Table Ptr of ENHANCEDSECCTL", __FUNCTION__, __LINE__);
		return GOS_FAIL;
	}

	pEntry = omci_timer_search(OMCI_ME_CLASS_ENHANCED_SECURITY_CONTROL,
						pEsc->EntityId);

	if (pEntry)
	{
		// should not existed, remove previous timer
		ret = omci_timer_delete_by_id(OMCI_ME_CLASS_ENHANCED_SECURITY_CONTROL,
						pEsc->EntityId);
		if (GOS_OK != ret)
		{
            OMCI_LOG(OMCI_LOG_LEVEL_ERR, "%s Delete timer for ESC AVC fail ret=%d", __FUNCTION__, ret);

			return GOS_FAIL;
		}
	}


	switch (state) {
	case ENHANCEDSECCTL_TIMER_STATE_AUTH_RESULT:
	case ENHANCEDSECCTL_TIMER_STATE_AUTH_STATUS:
		ret = omci_timer_create(OMCI_ME_CLASS_ENHANCED_SECURITY_CONTROL,
				pEsc->EntityId,
				0,
				100*1000000, FALSE, state, EnhancedSecCtlTmrHdlr, OMCI_TIMER_SIG_ARC);

	break;
	default:
		OMCI_LOG(OMCI_LOG_LEVEL_WARN, "Unknown state of ESC AVC");

		return GOS_FAIL;
	}
			
	if (GOS_OK != ret)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Create timer for ESC AVC fail");
		return GOS_FAIL;
	}

	return GOS_OK;
}

GOS_ERROR_CODE EnhancedSecCtlDumpMib(void *pData, MIB_TABLE_INFO_T *pTblInfo)
{
	MIB_TABLE_ENHANCEDSECCTL_T *pEnhancedSecCtl = (MIB_TABLE_ENHANCEDSECCTL_T*)pData;
	OMCI_PRINT("%s", "EnhancedSecCtl");

	OMCI_PRINT("EntityId: 0x%02x", pEnhancedSecCtl->EntityId);
	OltCryptoCapDump(pEnhancedSecCtl);
	OMCI_PRINT("OltChallSt: %d", pEnhancedSecCtl->OltChallSt);
	OMCI_PRINT("OnuSelCryptoCap: %d", pEnhancedSecCtl->OnuSelCryptoCap);
	OMCI_PRINT("OltRslSt: %d", pEnhancedSecCtl->OltRslSt);
	OMCI_PRINT("OnuAuthSt: %d", pEnhancedSecCtl->OnuAuthSt);
	MasterSessKeyNameDump(pEnhancedSecCtl);
	OMCI_PRINT("EffecKetLen: %d", pEnhancedSecCtl->EffecKetLen);
	OltRamChallTblDump(pEnhancedSecCtl);
	OnuRamChallTblDump(pEnhancedSecCtl);
	OnuAuthRslTblDump(pEnhancedSecCtl);
	OltAuthRslTblDump(pEnhancedSecCtl);
	BcastKeyTblDump(pEnhancedSecCtl);

	return GOS_OK;
}

static GOS_ERROR_CODE OltRamChallEntryOper(UINT8 *pReceiveEntry, MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
	oltRamChallTblEntry_t *pEntry,*pTmpEntry,*pTmp2Entry=NULL;
	omci_olt_ram_chall_entry_t *pRowEntry;
	//UINT32 tmpU32 = 0;

	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s...", __FUNCTION__);

	pEntry = (oltRamChallTblEntry_t*)malloc(sizeof(oltRamChallTblEntry_t));
	if(!pEntry)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR,"Malloc oltRamChall Table Entry Fail");
		return GOS_FAIL;
	}

	memcpy(&(pEntry->tableEntry),(omci_olt_ram_chall_entry_t*)pReceiveEntry,sizeof(omci_olt_ram_chall_entry_t));
	pRowEntry = &pEntry->tableEntry;
	if(0 == pRowEntry->rowNum)
	{
        OMCI_LOG(OMCI_LOG_LEVEL_DBG, "OltRamChallTbl rowNum = %d...", pRowEntry->rowNum);
        /* Row Number '0' is used to clear this talbe, remove all the existed entries */
        pTmpEntry = LIST_FIRST(&pEsc->oltRamChallHead);
        while (pTmpEntry != NULL)
        {
            OMCI_LOG(OMCI_LOG_LEVEL_DBG,"delete OltRamChallTbl entry");
            LIST_REMOVE(pTmpEntry, entries);
            free(pTmpEntry);
            pEsc->curOltRamChallCnt--;
            pTmpEntry = LIST_FIRST(&pEsc->oltRamChallHead);
        }
        LIST_INIT(&pEsc->oltRamChallHead);
		free(pEntry);
		return GOS_OK;		
	}

	/*compare this entry is exist or not*/
	LIST_FOREACH(pTmpEntry, &pEsc->oltRamChallHead, entries)
	{
		if(pRowEntry->rowNum == pTmpEntry->tableEntry.rowNum)
		{
			/*modify it */
            OMCI_LOG(OMCI_LOG_LEVEL_DBG, "modify OltRamChallTbl entry");
			memcpy(&(pTmpEntry->tableEntry), pRowEntry, sizeof(omci_olt_ram_chall_entry_t));
			free(pEntry);
			return GOS_OK;
		}
		pTmp2Entry = pTmpEntry;
	}

	/*not found, create new entry*/
	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "add OltRamChall entry");
	if(NULL == pTmp2Entry)
		LIST_INSERT_HEAD(&pEsc->oltRamChallHead, pEntry, entries);
	else
		LIST_INSERT_AFTER(pTmp2Entry, pEntry, entries);
	pEsc->curOltRamChallCnt++;

	return GOS_OK;
}

static GOS_ERROR_CODE OltAuthResultEntryOper(UINT8 *pReceiveEntry, MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
    oltAuthRslTblEntry_t *pEntry,*pTmpEntry,*pTmp2Entry=NULL;
    omci_olt_auth_rsl_entry_t *pRowEntry;

    OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s...", __FUNCTION__);

    pEntry = (oltAuthRslTblEntry_t *)malloc(sizeof(oltAuthRslTblEntry_t));
    if(!pEntry)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_ERR,"Malloc oltAuthRsl Table Entry Fail");
        return GOS_FAIL;
    }

    memcpy(&(pEntry->tableEntry),(omci_olt_auth_rsl_entry_t*)pReceiveEntry,sizeof(omci_olt_auth_rsl_entry_t));
    pRowEntry = &pEntry->tableEntry;
    if(0 == pRowEntry->rowNum)
    {
        OMCI_LOG(OMCI_LOG_LEVEL_DBG, "OltAuthRslTbl rowNum = %d...", pRowEntry->rowNum);
        /* Row Number '0' is used to clear this talbe, remove all the existed entries */
        pTmpEntry = LIST_FIRST(&pEsc->oltAuthRslHead);
        while (pTmpEntry != NULL)
        {
            OMCI_LOG(OMCI_LOG_LEVEL_DBG,"delete OltAuthRslTbl entry");
            LIST_REMOVE(pTmpEntry, entries);
            free(pTmpEntry);
            pEsc->curOltAuthRslCnt--;
            pTmpEntry = LIST_FIRST(&pEsc->oltAuthRslHead);
        }
        LIST_INIT(&pEsc->oltAuthRslHead);
        free(pEntry);
        return GOS_OK;
    }

    /*compare this entry is exist or not*/
    LIST_FOREACH(pTmpEntry, &pEsc->oltAuthRslHead, entries)
    {
        if(pRowEntry->rowNum == pTmpEntry->tableEntry.rowNum)
        {
            /*modify it */
            OMCI_LOG(OMCI_LOG_LEVEL_DBG, "modify OltAuthRslTbl entry");
            memcpy(&(pTmpEntry->tableEntry), pRowEntry, sizeof(omci_olt_auth_rsl_entry_t));
            free(pEntry);
            return GOS_OK;
        }
        pTmp2Entry = pTmpEntry;
    }

    /*not found, create new entry*/
    OMCI_LOG(OMCI_LOG_LEVEL_DBG, "add OltAuthRsl entry");
    if(NULL == pTmp2Entry)
        LIST_INSERT_HEAD(&pEsc->oltAuthRslHead, pEntry, entries);
    else
        LIST_INSERT_AFTER(pTmp2Entry, pEntry, entries);
    pEsc->curOltAuthRslCnt++;

    return GOS_OK;
}

static GOS_ERROR_CODE BcastKeyEntryOper(UINT8 *pReceiveEntry, MIB_TABLE_ENHANCEDSECCTL_T *pEsc)
{
	bcastKeyTblEntry_t *pEntry,*pTmpEntry,*pTmp2Entry=NULL;
	omci_bcast_key_entry_t *pRowEntry;
	omci_mc_key_info_t mcKey;

	pEntry = (bcastKeyTblEntry_t*)malloc(sizeof(bcastKeyTblEntry_t));
	if(!pEntry)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR,"Malloc bcastKey Table Entry Fail");
		return GOS_FAIL;
	}

	memcpy(&(pEntry->tableEntry),(omci_bcast_key_entry_t*)pReceiveEntry,sizeof(omci_bcast_key_entry_t));
	pRowEntry = &pEntry->tableEntry;
	if ((pRowEntry->rowCtl.bit.action > MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_CLEAR_TBL) || 
		(pRowEntry->rowId.bit.idx > 2) ||
		((pRowEntry->rowCtl.bit.action != MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_CLEAR_TBL) && (pRowEntry->rowId.bit.idx < 1)))
	{
		OMCI_LOG(OMCI_LOG_LEVEL_ERR, "Invalid BcastKey Action or Key Index, rowCtl=%d, rowId=%d", pRowEntry->rowCtl.val, pRowEntry->rowId.val);
		free(pEntry);
		return GOS_FAIL;
	}

	if (MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_CLEAR_TBL == pRowEntry->rowCtl.bit.action)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_DBG,"Clear both bcastKey Table Entry");

		/* clear the key on ASIC */
		memset(&mcKey, 0, sizeof(omci_mc_key_info_t));
		mcKey.action = MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_CLEAR_TBL;
		omci_wrapper_setMcKey(&mcKey);

		/* clear the key and delete it*/
        pTmpEntry = LIST_FIRST(&pEsc->bcastKeyHead);
        while (pTmpEntry != NULL)
        {
            LIST_REMOVE(pTmpEntry, entries);
            free(pTmpEntry);
            pEsc->curBcastKeyCnt--;
            pTmpEntry = LIST_FIRST(&pEsc->bcastKeyHead);
        }
        LIST_INIT(&pEsc->bcastKeyHead);
        
		free(pEntry);
		return GOS_OK;		
	}
	else if (MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_CLEAR == pRowEntry->rowCtl.bit.action)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_INFO,"Clear bcastKey Table Entry[%d]", pRowEntry->rowId.bit.idx);
		LIST_FOREACH(pTmpEntry,&pEsc->bcastKeyHead,entries)
		{
			if(pRowEntry->rowId.bit.idx == pTmpEntry->tableEntry.rowId.bit.idx)
			{
				/* clear the key on ASIC */
				memset(&mcKey, 0, sizeof(omci_mc_key_info_t));
				mcKey.idx = pRowEntry->rowId.bit.idx;
				mcKey.action = MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_CLEAR;
				omci_wrapper_setMcKey(&mcKey);

				LIST_REMOVE(pTmpEntry,entries);
				free(pTmpEntry);
				pEsc->curBcastKeyCnt--;
				free(pEntry);
				return GOS_OK;		
			}
		}
        free(pEntry);
		/* no matched Key index */
		OMCI_LOG(OMCI_LOG_LEVEL_WARN,"Clear bcastKey Table Entry[%d]: no mached", pRowEntry->rowId.bit.idx);
		return GOS_OK;
	}
	else if (MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_SET == pRowEntry->rowCtl.bit.action)
	{
		OMCI_LOG(OMCI_LOG_LEVEL_INFO,"Set bcastKey Table Entry[%d]", pRowEntry->rowId.bit.idx);
		LIST_FOREACH(pTmpEntry,&pEsc->bcastKeyHead,entries)
		{
			if(pRowEntry->rowId.bit.idx == pTmpEntry->tableEntry.rowId.bit.idx)
			{
				/* existed key: update it*/
				memcpy(&(pTmpEntry->tableEntry), pRowEntry, sizeof(omci_bcast_key_entry_t));

				/* set the key on ASIC */
				memset(&mcKey, 0, sizeof(omci_mc_key_info_t));
				mcKey.idx = pRowEntry->rowId.bit.idx;
				mcKey.action = MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_SET;
				memcpy(mcKey.key, pRowEntry->content, MIB_TABLE_BCASTKEY_CONTENT_LEN);

				OMCI_LOG(OMCI_LOG_LEVEL_DBG,"update bcastKeyTbl entry[%d], flag[%d]", pTmpEntry->tableEntry.rowId.bit.idx, mcKey.action);

				omci_wrapper_setMcKey(&mcKey);

				free(pEntry);
				return GOS_OK;		
			}
			pTmp2Entry = pTmpEntry;
		}

		/* set the key on ASIC */
		memset(&mcKey, 0, sizeof(omci_mc_key_info_t));
		mcKey.idx = pRowEntry->rowId.bit.idx;
		mcKey.action = MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_SET;
		memcpy(mcKey.key, pRowEntry->content, MIB_TABLE_BCASTKEY_CONTENT_LEN);
		omci_wrapper_setMcKey(&mcKey);

		/* no matched Key index: create a new one */
		OMCI_LOG(OMCI_LOG_LEVEL_INFO, "add BcastkeyTbl entry");
		if(NULL == pTmp2Entry)
			LIST_INSERT_HEAD(&pEsc->bcastKeyHead, pEntry, entries);
		else
			LIST_INSERT_AFTER(pTmp2Entry, pEntry, entries);
		pEsc->curBcastKeyCnt++;
		return GOS_OK;

	}

	OMCI_LOG(OMCI_LOG_LEVEL_ERR, "%s: no matched action, should not reach here.", __FUNCTION__);
	return GOS_FAIL;
}

GOS_ERROR_CODE EnhancedSecCtlDrvCfg(void* pOldRow, void* pNewRow, MIB_OPERA_TYPE operationType, MIB_ATTRS_SET attrSet, UINT32 pri)
{
	MIB_TABLE_ENHANCEDSECCTL_T *pEsc, *pMibEsc, tmpEsc;
	MIB_ATTR_INDEX		attrIndex;
	GOS_ERROR_CODE		ret = GOS_OK;

	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "Start %s...", __FUNCTION__);

	pEsc = (MIB_TABLE_ENHANCEDSECCTL_T*)pNewRow;
	tmpEsc.EntityId = pEsc->EntityId;

	if(!mib_FindEntry(MIB_TABLE_ENHANCEDSECCTL_INDEX,&tmpEsc,&pMibEsc))
	{
		OMCI_LOG(OMCI_LOG_LEVEL_WARN,"can't find EhnSecCtrl 0x%x",pEsc->EntityId);
		return GOS_OK;
	}
	
	switch (operationType){
	case MIB_ADD:
		/*add for table type attribute*/
		LIST_INIT(&pMibEsc->oltRamChallHead);
		pMibEsc->curOltRamChallCnt = 0;
		LIST_INIT(&pMibEsc->onuRamChallHead);
		pMibEsc->curOnuRamChallCnt = 0;
		LIST_INIT(&pMibEsc->onuAuthRslHead);
		pMibEsc->curOnuAuthRslCnt = 0;
		LIST_INIT(&pMibEsc->oltAuthRslHead);
		pMibEsc->curOltAuthRslCnt = 0;
		LIST_INIT(&pMibEsc->bcastKeyHead);
		pMibEsc->curBcastKeyCnt = 0;

		break;
	case MIB_SET:
		if (MIB_IsInAttrSet(&attrSet, MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX))
		{
			if ((TRUE == pEsc->OltChallSt))
			{

				/* prepare the ramdon challenge table and calculate the authentication result */
				EnhancedSecCtlRsl(pMibEsc);

				/* create timer for send AVC of  onu authentication result table & random challenge table */
				EnhancedSecCtlTmrProc(pMibEsc, ENHANCEDSECCTL_TIMER_STATE_AUTH_RESULT);
			}
		}
	
		if (MIB_IsInAttrSet(&attrSet, MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX))
		{
			pMibEsc->OltChallSt = FALSE;
			pMibEsc->OnuAuthSt = 0;
		}
			
		if (MIB_IsInAttrSet(&attrSet, MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX))
		{
			OltRamChallEntryOper(pEsc->OltRamChallTbl, pMibEsc);
			pMibEsc->OltChallSt = FALSE;
			pMibEsc->OnuAuthSt = 0;
		}
		
		if (MIB_IsInAttrSet(&attrSet, MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX))
		{
            OltAuthResultEntryOper(pEsc->OltAuthRslTbl, pMibEsc);
		}
		
		if (MIB_IsInAttrSet(&attrSet, MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX))
		{
            if ((TRUE == pEsc->OltRslSt))
			{
                /* Verify if OLT's auth result is correct or not. */
                pMibEsc->OnuAuthSt = ONU_AUTH_RESULT_FAILURE;
                if(GOS_OK == OltAuthRslChk(pMibEsc))
                    pMibEsc->OnuAuthSt = ONU_AUTH_RESULT_SUCCESS;

                /* Authentication result: set auth status and send AVC */
                pMibEsc->OltRslSt = pEsc->OltRslSt;
                EnhancedSecCtlTmrProc(pMibEsc, ENHANCEDSECCTL_TIMER_STATE_AUTH_STATUS);
			}
		}

		if (MIB_IsInAttrSet(&attrSet, MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX))
		{
			ret = BcastKeyEntryOper(pEsc->BcastKeyTbl, pMibEsc);
		}

    	break;
	case MIB_GET:
		attrIndex = MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX;
		if (MIB_IsInAttrSet(&attrSet, attrIndex))
		{
		    UINT32 attrSize = MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN * (pMibEsc->curOnuRamChallCnt);
			onuRamChallTblEntry_t *pOnuRamChallEntry = NULL;
			UINT8 *ptr = NULL;

            ptr  = gOmciMulGetData[pri].attribute[attrIndex].pAttrValue; 
            if(ptr && attrSize != gOmciMulGetData[pri].attribute[attrIndex].attrSize)/*attrSize change , first free old buffer */
            {
                OMCI_LOG(OMCI_LOG_LEVEL_WARN, "%s() %d ptr=0x%p, size= %u", __FUNCTION__, __LINE__, ptr, gOmciMulGetData[pri].attribute[attrIndex].attrSize);
                gOmciMulGetData[pri].attribute[attrIndex].pAttrValue = NULL;
				free(ptr);
                ptr = NULL;
            }
            
            if(NULL == ptr && attrSize>0) /*Allocate new buffer */
            {
                gOmciMulGetData[pri].attribute[attrIndex].pAttrValue = (UINT8 *)malloc(attrSize);
                ptr  = gOmciMulGetData[pri].attribute[attrIndex].pAttrValue; 
                OMCI_LOG(OMCI_LOG_LEVEL_WARN, "%s() %d ptr=0x%p, size= %u", __FUNCTION__, __LINE__, ptr, attrSize);  
            }


             OMCI_LOG(OMCI_LOG_LEVEL_WARN, "%s() %d ptr=0x%p, size= %u", __FUNCTION__, __LINE__, ptr, attrSize); 
            gOmciMulGetData[pri].attribute[attrIndex].attrSize = attrSize;
            gOmciMulGetData[pri].attribute[attrIndex].attrIndex = attrIndex;
            gOmciMulGetData[pri].attribute[attrIndex].doneSeqNum = 0;
            gOmciMulGetData[pri].attribute[attrIndex].maxSeqNum =
                    (gOmciMulGetData[pri].attribute[attrIndex].attrSize + OMCI_MSG_BASELINE_GET_NEXT_RSP_LIMIT - 1) / OMCI_MSG_BASELINE_GET_NEXT_RSP_LIMIT;
            if(ptr)
            {                              
                LIST_FOREACH(pOnuRamChallEntry, &pMibEsc->onuRamChallHead, entries)
                {
                    memcpy(ptr, &pOnuRamChallEntry->tableEntry, MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN);
                    ptr += MIB_TABLE_ONURAMCHALLTABLE_CONTENT_LEN;
                }

            }

		}

		attrIndex = MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX;
		if (MIB_IsInAttrSet(&attrSet, attrIndex))
		{
		    UINT32 attrSize = MIB_TABLE_ONUAUTHRSLTABLE_LEN * (pMibEsc->curOnuAuthRslCnt);
			onuAuthRslTblEntry_t *pOnuAuthRslEntry = NULL;
			UINT8 *ptr = NULL;
			BOOL hasPsk = TRUE;
			UINT8 zeroArray[16]={0};
			
			if(0 == memcmp(gInfo.psk, &zeroArray, 16))
				hasPsk =FALSE;
	
            ptr  = gOmciMulGetData[pri].attribute[attrIndex].pAttrValue; 
            if(ptr && attrSize != gOmciMulGetData[pri].attribute[attrIndex].attrSize)/*attrSize change , first free old buffer */
            {
                OMCI_LOG(OMCI_LOG_LEVEL_WARN, "%s() %d ptr=0x%p, size= %u", __FUNCTION__, __LINE__, ptr, gOmciMulGetData[pri].attribute[attrIndex].attrSize);
                gOmciMulGetData[pri].attribute[attrIndex].pAttrValue = NULL;
				free(ptr);
                ptr = NULL;
            }
            
            if(NULL == ptr && attrSize>0) /*Allocate new buffer */
            {
                gOmciMulGetData[pri].attribute[attrIndex].pAttrValue = (UINT8 *)malloc(attrSize);
                ptr  = gOmciMulGetData[pri].attribute[attrIndex].pAttrValue; 
                OMCI_LOG(OMCI_LOG_LEVEL_WARN, "%s() %d ptr=0x%p, size= %u", __FUNCTION__, __LINE__, ptr, attrSize);   
            }

            gOmciMulGetData[pri].attribute[attrIndex].attrSize = attrSize;
            gOmciMulGetData[pri].attribute[attrIndex].attrIndex = attrIndex;
            gOmciMulGetData[pri].attribute[attrIndex].doneSeqNum = 0;
            gOmciMulGetData[pri].attribute[attrIndex].maxSeqNum =
                (gOmciMulGetData[pri].attribute[attrIndex].attrSize + OMCI_MSG_BASELINE_GET_NEXT_RSP_LIMIT - 1) / OMCI_MSG_BASELINE_GET_NEXT_RSP_LIMIT;

            if(ptr)
            {	
                LIST_FOREACH(pOnuAuthRslEntry, &pMibEsc->onuAuthRslHead, entries)
                {
                    if(hasPsk)
                        memcpy(ptr, &pOnuAuthRslEntry->tableEntry, MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN);			   				
                    else	
                        memset(ptr, 0x0, MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN);

                    ptr += MIB_TABLE_ONUAUTHRSLTABLE_CONTENT_LEN;
                }
            }
		}


	   	break;
    default:
    	return GOS_FAIL;
    	break;
    }

    return ret;
}
GOS_ERROR_CODE mibTable_init(MIB_TABLE_INDEX tableId)
{
    gMibEnhancedSecCtlTableInfo.Name = "EnhancedSecCtl";
    gMibEnhancedSecCtlTableInfo.ShortName = "EHSECCTL";
    gMibEnhancedSecCtlTableInfo.Desc = "Enhanced security control";
    gMibEnhancedSecCtlTableInfo.ClassId = (UINT32)(OMCI_ME_CLASS_ENHANCED_SECURITY_CONTROL);
    gMibEnhancedSecCtlTableInfo.InitType = (UINT32)(OMCI_ME_INIT_TYPE_ONU);
    gMibEnhancedSecCtlTableInfo.StdType = (UINT32)(OMCI_ME_TYPE_STANDARD);
    gMibEnhancedSecCtlTableInfo.ActionType = (UINT32)(OMCI_ME_ACTION_SET | OMCI_ME_ACTION_GET | OMCI_ME_ACTION_GET_NEXT);
    gMibEnhancedSecCtlTableInfo.pAttributes = &(gMibEnhancedSecCtlAttrInfo[0]);

    gMibEnhancedSecCtlTableInfo.attrNum = MIB_TABLE_ENHANCEDSECCTL_ATTR_NUM;
    gMibEnhancedSecCtlTableInfo.entrySize = sizeof(MIB_TABLE_ENHANCEDSECCTL_T);
    gMibEnhancedSecCtlTableInfo.pDefaultRow = &gMibEnhancedSecCtlDefRow;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].Name = "EntityId";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OltCryptoCap";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OltRamChallTable";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OltChallSt";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OnuSelCryptoCap";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OnuRamChallTable";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OnuAuthRslTable";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OltAuthRslTable";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OltRslSt";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].Name = "OnuAuthSt";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].Name = "MasterSessKeyName";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].Name = "BcastKey";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].Name = "EffecKetLen";

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Entity ID";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "OLT crypto capabilities";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "OLT random challenge table";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "OLT challenge status";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "ONU selected crypto capabilities";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "ONU random challenge table";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "ONU authentication result table";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "OLT authentication result table";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "OLT result status";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "ONU authentication state";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Master session key name";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Bcast key table";
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].Desc = "Effective key length";

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT16;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_ARRAY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_TABLE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT8;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT8;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_TABLE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_TABLE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_TABLE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT8;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT8;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_STR;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_TABLE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].DataType = MIB_ATTR_TYPE_UINT16;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].Len = 2;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].Len = 16;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Len = 17;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].Len = 1;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].Len = 1;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Len = 16;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Len = 16;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].Len = 17;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].Len = 1;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].Len = 1;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].Len = 16;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].Len = 18;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].Len = 2;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].IsIndex = FALSE;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].MibSave = TRUE;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_HEX;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].OutStyle = MIB_ATTR_OUT_DEC;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].OltAcc = OMCI_ME_ATTR_ACCESS_READ;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].AvcFlag = FALSE;

    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ENTITYID_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTCHALLST_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUSELCRYPTOCAP_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONURAMCHALLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTAUTHRSLTABLE_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_OLTRSLST_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_ONUAUTHST_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_MASTERSESSKEYNAME_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_BCASTKEY_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    gMibEnhancedSecCtlAttrInfo[MIB_TABLE_ENHANCEDSECCTL_EFFECKETLEN_INDEX - MIB_TABLE_FIRST_INDEX].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    gMibEnhancedSecCtlDefRow.EntityId = 0;
    memset(gMibEnhancedSecCtlDefRow.OltCryptoCap, 0x00, sizeof(gMibEnhancedSecCtlDefRow.OltCryptoCap));
    memset(gMibEnhancedSecCtlDefRow.OltRamChallTbl, 0x00, sizeof(gMibEnhancedSecCtlDefRow.OltRamChallTbl));
    gMibEnhancedSecCtlDefRow.OltChallSt = 0;
    gMibEnhancedSecCtlDefRow.OnuSelCryptoCap = 1;	/* support AES-CMAC-128 (support is mandatory) by default */
    memset(gMibEnhancedSecCtlDefRow.OnuRamChallTbl, 0x00, sizeof(gMibEnhancedSecCtlDefRow.OnuRamChallTbl));
    memset(gMibEnhancedSecCtlDefRow.OnuAuthRslTbl, 0x00, sizeof(gMibEnhancedSecCtlDefRow.OnuAuthRslTbl));
    memset(gMibEnhancedSecCtlDefRow.OltAuthRslTbl, 0x00, sizeof(gMibEnhancedSecCtlDefRow.OltAuthRslTbl));
    gMibEnhancedSecCtlDefRow.OltRslSt = 0;
    gMibEnhancedSecCtlDefRow.OnuAuthSt = 0;
    memset(gMibEnhancedSecCtlDefRow.MasterSessKeyName, 0x00, sizeof(gMibEnhancedSecCtlDefRow.MasterSessKeyName));
    memset(gMibEnhancedSecCtlDefRow.BcastKeyTbl, 0x00, sizeof(gMibEnhancedSecCtlDefRow.BcastKeyTbl));
    gMibEnhancedSecCtlDefRow.EffecKetLen = 0;

	/*add for table type attribute*/
	LIST_INIT(&gMibEnhancedSecCtlDefRow.oltRamChallHead);
	gMibEnhancedSecCtlDefRow.curOltRamChallCnt= 0;
	LIST_INIT(&gMibEnhancedSecCtlDefRow.onuRamChallHead);
	gMibEnhancedSecCtlDefRow.curOnuRamChallCnt= 0;
	LIST_INIT(&gMibEnhancedSecCtlDefRow.onuAuthRslHead);
	gMibEnhancedSecCtlDefRow.curOnuAuthRslCnt= 0;
	LIST_INIT(&gMibEnhancedSecCtlDefRow.oltAuthRslHead);
	gMibEnhancedSecCtlDefRow.curOltAuthRslCnt= 0;
	LIST_INIT(&gMibEnhancedSecCtlDefRow.bcastKeyHead);
	gMibEnhancedSecCtlDefRow.curBcastKeyCnt= 0;

    gMibEnhancedSecCtlOper.meOperDrvCfg = EnhancedSecCtlDrvCfg;
    gMibEnhancedSecCtlOper.meOperConnCheck = NULL;
    gMibEnhancedSecCtlOper.meOperDump = EnhancedSecCtlDumpMib;
    gMibEnhancedSecCtlOper.meOperConnCfg = NULL;
    gMibEnhancedSecCtlOper.meOperAvlTreeAdd = NULL;
    gMibEnhancedSecCtlOper.meOperAlarmHandler = NULL;
    gMibEnhancedSecCtlOper.meOperTestHandler = NULL;

    MIB_TABLE_ENHANCEDSECCTL_INDEX = tableId;
    MIB_InfoRegister(tableId, &gMibEnhancedSecCtlTableInfo, &gMibEnhancedSecCtlOper);

    return GOS_OK;
}

